package hust.music.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import hust.music.dao.JpaDao;
import hust.music.entity.User;
import hust.music.service.UserModService;
import hust.music.util.DynamicInfo;

/**
 * UserModServiceImpl
 * 
 * @author Administrator
 * @since 1.0
 */
@Service("userModService")
@Transactional
public class UserModServiceImpl implements UserModService {

	@Autowired
	private JpaDao jpaDao;

	/**
	 * 用户注册
	 * 
	 * @param user 用户实体
	 * @return 用户实体
	 * @throws Exception
	 */
	@Override
	public User register(User user) {
		List<User> list = jpaDao.findAll(User.class,
				"from User where username = ?1", user.getUsername());
		if (list.size() == 0) {
			jpaDao.save(user);
			return user;
		}
		return null;
	}

	/**
	 * 用户登录
	 * 
	 * @param user 用户实体
	 * @return 用户实体
	 * @throws Exception
	 */
	@Override
	public User login(User user) {
		List<User> list = jpaDao.findAll(User.class,
				"from User where username = ?1", user.getUsername());
		if (list.size() == 1) {
			User login = user = list.get(0);
			if (user.getAuthtoken().equals(user.getAuthtoken())) {
				login.setLastAuthority(new Date());
				return login;
			}
			return null;
		}
		return null;
	}

	/**
	 * 查询用户
	 * 
	 * @param id 用户Id
	 * @return 用户
	 */
	@Override
	public User get(Integer id) {
		return jpaDao.get(User.class, id);
	}

	/**
	 * 我关注的人
	 * 
	 * @param user 用户
	 * @param firstResult 开始记录
	 * @param maxResult 查询记录数
	 * @return 我关注的人
	 */
	@Override
	public List<DynamicInfo> getFollow(User user, int firstResult, int maxResult) {
		List<User> follows = jpaDao
				.findByPage(
						User.class,
						"select u from User u, FollowRel f where f.initiativeFollow = ? and f.passiveFollow = u order by f.time desc",
						user, firstResult, maxResult);

		List<DynamicInfo> list = new ArrayList<DynamicInfo>();

		for (User u : follows) {
			int follow = jpaDao
					.findAll(
							User.class,
							"select u from User u, FollowRel f where f.initiativeFollow = ? and f.passiveFollow = u",
							user).size();
			int fan = jpaDao
					.findAll(
							User.class,
							"select u from User u, FollowRel f where f.initiativeFollow = u and f.passiveFollow = ?",
							user).size();
			DynamicInfo dynamicInfo = new DynamicInfo(u.getId(), u
					.getNickname(), u.getAvatar(), follow, u.getSex(), null,
					fan, null, u.getIntroduce(), null, null, null);
			list.add(dynamicInfo);
		}

		return list;
	}

	/**
	 * 我关注的人的总数
	 * 
	 * @param user 用户
	 * @return 我关注的人的总数
	 */
	@Override
	public int getTotalFollow(User user) {
		return jpaDao
				.findAll(
						User.class,
						"select u from User u, FollowRel f where f.initiativeFollow = ? and f.passiveFollow = u",
						user).size();
	}

	/**
	 * 我的粉丝
	 * 
	 * @param user 用户
	 * @param firstResult 开始记录
	 * @param maxResult 查询记录数
	 * @return 我的粉丝
	 */
	@Override
	public List<DynamicInfo> getFan(User user, int firstResult, int maxResult) {
		List<User> follows = jpaDao
				.findByPage(
						User.class,
						"select u from User u, FollowRel f where f.initiativeFollow = u and f.passiveFollow = ? order by f.time desc",
						user, firstResult, maxResult);

		List<DynamicInfo> list = new ArrayList<DynamicInfo>();

		for (User u : follows) {
			int follow = jpaDao
					.findAll(
							User.class,
							"select u from User u, FollowRel f where f.initiativeFollow = ? and f.passiveFollow = u",
							user).size();
			int fan = jpaDao
					.findAll(
							User.class,
							"select u from User u, FollowRel f where f.initiativeFollow = u and f.passiveFollow = ?",
							user).size();
			DynamicInfo dynamicInfo = new DynamicInfo(u.getId(), u
					.getNickname(), u.getAvatar(), follow, u.getSex(), null,
					fan, null, u.getIntroduce(), null, null, null);
			list.add(dynamicInfo);
		}

		return list;
	}

	/**
	 * 我的粉丝总数
	 * 
	 * @param user 用户
	 * @return 我的粉丝总数
	 */
	@Override
	public int getTotalFan(User user) {
		return jpaDao
				.findAll(
						User.class,
						"select u from User u, FollowRel f where f.initiativeFollow = u and f.passiveFollow = ?",
						user).size();
	}

	/**
	 * 我的好友
	 * 
	 * @param user 用户
	 * @param firstResult 开始记录
	 * @param maxResult 查询记录数
	 * @return 我的好友
	 */
	@Override
	public List<DynamicInfo> getFriend(User user, int firstResult, int maxResult) {
		List<User> friends = jpaDao.findByPage(User.class,
				"select u from User u, FriendRel f where ( f.user = ? "
						+ "and f.friend = u ) or ( f.friend = ? "
						+ "and f.user = u ) order by f.time desc",
				new Object[] { user, user }, firstResult, maxResult);

		List<DynamicInfo> list = new ArrayList<DynamicInfo>();

		for (User u : friends) {
			int follow = jpaDao
					.findAll(
							User.class,
							"select u from User u, FollowRel f where f.initiativeFollow = ? and f.passiveFollow = u",
							user).size();
			int fan = jpaDao
					.findAll(
							User.class,
							"select u from User u, FollowRel f where f.initiativeFollow = u and f.passiveFollow = ?",
							user).size();
			DynamicInfo dynamicInfo = new DynamicInfo(u.getId(), u
					.getNickname(), u.getAvatar(), follow, u.getSex(), null,
					fan, null, u.getIntroduce(), null, null, null);
			list.add(dynamicInfo);
		}

		return list;
	}

	/**
	 * 我的好友总数
	 * 
	 * @param user 用户
	 * @return 我的好友总数
	 */
	@Override
	public int getTotalFriend(User user) {
		return jpaDao.findAll(
				User.class,
				"select u from User u, FriendRel f where ( f.user = ? "
						+ "and f.friend = u ) or ( f.friend = ? "
						+ "and f.user = u ) order by f.time desc",
				new Object[] { user, user }).size();
	}

	@Override
	public void update(User user) {
		jpaDao.update(user);
	}

}